package de.tudarmstadt.ukp.muip.fibqdojo.rule;

import java.util.HashMap;
import java.util.List;

import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQRule;
import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQSentence;
import de.tudarmstadt.ukp.muip.fibqdojo.interfaces.IFIBQSentenceToken;
import de.tudarmstadt.ukp.muip.fibqdojo.visitor.GetFormVisitor;
import de.tudarmstadt.ukp.muip.fibqdojo.visitor.GetSentenceTokenListVisitor;

/**
 * Filters a sentence if it <b>contains</b> fully or partial one of the provided
 * constants. That means that each token's form is compared with the constants
 * as well as each <b>character<b> of a token .
 * 
 * @author Tobias Horsmann
 */
public class ConstantsMatchingRule implements IFIBQRule {

    private HashMap<String, String> map = new HashMap<String, String>();

    public ConstantsMatchingRule(List<String> constants) {
        for (String constant : constants) {
            map.put(constant, "");
        }
    }

    @Override
    public boolean match(IFIBQSentence sentence) {
        GetSentenceTokenListVisitor visitor = new GetSentenceTokenListVisitor();
        sentence.accept(visitor);
        for (IFIBQSentenceToken token : visitor.getSentenceTokens()) {
            boolean didMatch = match(token);
            if (didMatch) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean match(IFIBQSentenceToken sentenceToken) {
        GetFormVisitor formVisitor = new GetFormVisitor();
        sentenceToken.accept(formVisitor);
        String form = formVisitor.getForm();

        if (map.containsKey(form.toLowerCase())) {
            return true;
        }
        String lowerCased = form.toLowerCase();
        for (int i = 0; i < form.length(); i++) {
            String currentChar = new String(lowerCased.charAt(i) + "");
            if (map.get(currentChar) != null) {
                return true;
            }
        }
        return false;
    }

}
